<?php
// TODO: song    登录接口
namespace Home\Common\Api;

class UserApi
{


    /**
     * 检测用户是否登录
     * @return integer 0-未登录，大于0-当前登录用户ID
     */
public  function is_login(){
        $users = session('user_id');
        if (empty($users)) {
            return 0;
        } else {
            return $users;
        }
}

    // TODO: 获取验证码
    public function get_verification_code($w,$h)
    {

      $im = imagecreate($w, $h);

      //imagecolorallocate($im, 14, 114, 180); // background color
      $red = imagecolorallocate($im, 255, 0, 0);
      $white = imagecolorallocate($im, 255, 255, 255);

      $num1 = rand(1, 20);
      $num2 = rand(1, 20);

      $_SESSION['code'] = $num1 + $num2;

      $gray = imagecolorallocate($im, 118, 151, 199);
      $black = imagecolorallocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));

      //画背景
      imagefilledrectangle($im, 0, 0, 100, 24, $black);
      //在画布上随机生成大量点，起干扰作用;
      for ($i = 0; $i < 80; $i++) {
        imagesetpixel($im, rand(0, $w), rand(0, $h), $gray);
      }

      imagestring($im, 5, 5, 4, $num1, $red);
      imagestring($im, 5, 30, 3, "+", $red);
      imagestring($im, 5, 45, 4, $num2, $red);
      imagestring($im, 5, 70, 3, "=", $red);
      imagestring($im, 5, 80, 2, "?", $white);

      header("Content-type: image/png");
      imagepng($im);
      imagedestroy($im);
    }

// TODO: 过滤特殊字符
    function replaceSpecialChar($strParam){
    $regex = "/\/|\~|\!\#|\\$|\%|\^|\&|\*|\(|\)|\_|\+|\{|\}|\:|\<|\>|\?|\[|\]|\,|\/|\;|\'|\`|\-|\=|\\\|\|/";
    return preg_replace($regex,"",$strParam);
}


// TODO: 查询用户资料
    function getUserList($data){

      $where ="`name` = '".$data['name']."'";

      if($data['name'] ==''){
        $where ="email = '".$data['email']."'";
      }

      $list = D('user')->where($where)->find();

   return $list;
}


// TODO: 查询用户资料
    function sessionGetUserList($data){
      $where['id'] =session('user_id');
      $list = D('user')->where($where)->find();
      return $list;
}


// TODO: 登录成功后修改状态 或者退出登录修改状态
  function saveUser($state,$data){
    if($state =='login'){
        $save['last_login_time'] = date('Y-m-d H:i:s');
          $save['status'] = '1';
    }elseif($state =='logout'){
       $save['status'] = '0';
    }elseif($state =='revise_pwd'){
      $save['pwd'] = $data;
    }
     $where['id'] = session('user_id');
     $res = D('user')->where($where)->save($save);
   if($res){
    return ture;
   }
   return fasle;
  }

// TODO: 验证码验证
  function check_code($code){
   $old_code = session('code');
   if($old_code != $code){
    return false;
   }
   return ture;
  }


/**
 * 检查用户名是否符合规定
 *
 * @param STRING $username 要检查的用户名
 * @return TRUE or FALSE
 */
function is_username($username)
{
$strlen = strlen($username);
if (!preg_match("/^[a-zA-Z0-9_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+$/",
$username)) //开源软件:phpfensi.com
{
return false;
} elseif (20 < $strlen || $strlen < 2)
{
return false;
}
return true;
}

/**
 * 检查密码是否符合规定
 *
 * @param STRING $username 要检查的用户名
 * @return TRUE or FALSE
 */

function isPWD($value,$minLen=5,$maxLen=16){
  $match='/^[\\~!@#$%^&*()-_=+|{}\[\],.?\/:;\'\"\d\w]{'.$minLen.','.$maxLen.'}$/';
  $v = trim($value);
  $res = preg_match($match,$v);
  if($res) {
   return true;
  }
   return false;
}

/**
 * 检查邮箱是否符合规定
 *
 * @param STRING $username 要检查的用户名
 * @return TRUE or FALSE
 */
function isEmail($value)
{
  $match='/^[\w\d]+[\wd-.]*@[w\d-.]+\.[\w\d]{2,10}$/i';
  $v = trim($value);
  $res = preg_match($match,$v);
  if($res) {
   return true;
  }
   return false;
}

/**
 * 检查用户名或密码是否存在
 *
 * @param STRING  要检查的用户名
 * @return TRUE or FALSE
 */


function userIsExistence($username,$email)
{
  $res = D('user')->where('`name`="'.$username.'"')->find();
  if($res){
    return "用户名已存在，请重新填写！";
  }
  $res2 = D('user')->where('email="'.$email.'"')->find();
  if($res2) {
   return "电子邮箱已存在，请重新填写！";
  }
   return false;
}

/**
 * 检查用户名是否存在
 *
 * @param STRING  要检查的用户名
 * @return TRUE or FALSE
 */


function userIsNameExistence($username)
{
  $res = D('user')->where('`name`="'.$username.'"')->find();
  if($res){
    return false;
  }
   return ture;
}


/**
 * 检查邮箱是否存在
 *
 * @param STRING  要检查的用户名
 * @return TRUE or FALSE
 */

function userIsEmailExistence($email)
{
  $res2 = D('user')->where('email="'.$email.'"')->find();
  if($res2) {
   return false;
  }
   return ture;
}

//添加用户信息
function addUserList($username,$password,$email){
  $data =array();
  $random = $this->random();
  $data['pwd'] = sha1($password.$random);
  $data['name'] = $username;
  $data['email'] = $email;
  $data['random'] = $random;
  $data['is_activity'] = 1;
  $data['add_time'] = $data['last_login_time'] = date('Y-m-d H:i:s');
  $data['is_activity'] = 1;
  $data['describe'] = $data['face'] ='';
  $data['status'] = 1;
  $res = D('user')->add($data);
  if($res){
    return $res;
  }
  return false;
}


//随机
function random($length = 4 , $numeric = 0) {
    PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
    if($numeric) {
        $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
    } else {
        $hash = '';
        $chars = '123456789';
        $max = strlen($chars) - 1;
        for($i = 0; $i < $length; $i++) {
            $hash .= $chars[mt_rand(0, $max)];
        }
    }
    return $hash;
}


//获取用户资料
  function   get_user_list(){
   $users = D('User')->where('id = '.$_SESSION['user_id'])->find();
   if($users){
    return $users;
   }

  }



//更新用户资料
  function   save_user_list($data){
    if(!empty($data['pwd'])){
       $old =$this->sessionGetUserList();
       $data['pwd'] = sha1($data['pwd'].$old['random']);
    }
// print_r($data);die();
   $res = D('user')->where('id = '.$_SESSION['user_id'])->save($data);
   if(!$res){
      return false;
   }
  return $res;
  }



//检验密码是否正确
  public function is_pwd_true($pwd){
    $res = D('user')->field('pwd,random')->where('id = '.$_SESSION['user_id'])->find();
    $check_pwd = sha1($pwd.$res['random']);
    
    if($check_pwd == $res['pwd']){
      return ture;
    }
    return flase;
  }


  //发邮箱给用户重置密码
  public function reset_pwd(){
    $data = array();
      $res = D('user')->field('email,name')->where('id = '.$_SESSION['user_id'])->find();
      $data['msg'] = setEmail($res['email'],$res['name']);
      $data['url'] = gotomail($res['email']);
      return $data;
  }

}

?>
